/*
 * Text.cpp
 *
 *  Created on: 04/03/2013
 *      Author: eusyar
 */

#include "component/Text.h"
#include "component/Doodle.h"
#include "core/GameConteiner.h"
#include <iostream>

using namespace eats_vars;
using namespace std;

Text::Text(SDL_Surface * workingSurface, string fontPath, int fontSize, string * textToWrite, Point * position) {
	_fontPath = fontPath;
	_fontSize = fontSize;
	_textToWrite = textToWrite;

	_position = position;
	_workingSurface = workingSurface;
}

Text::~Text() {

}

bool Text::setUp() {

	bool ret = true;

	_color = new Color;
	_color->r = 0;
	_color->g = 0;
	_color->b = 0;
	_color->alfa = 0;

	_font = loadfont(_fontPath.c_str(), _fontSize);
	if(_font == NULL){
		return false;
	}

	_glyph = drawtext(_font,_color,_textToWrite->c_str(), blended);
	if(_glyph == NULL){
		return false;
	}

	return ret;
}

bool Text::render() {
	bool ret = true;
	Doodle::drawIMG(_workingSurface, _glyph, _position->x, _position->y);
	return ret;
}

bool Text::update() {
	bool ret = true;
	///ISSSO TA MUITO ERRADO

	_glyph = drawtext(_font,_color,_textToWrite->c_str(), blended);
	return ret;
}

bool Text::tearDown() {
	bool ret = true;
	SDL_FreeSurface(_glyph);
	TTF_CloseFont(_font);
	return ret;
}

TTF_Font* Text::loadfont(const char* file, int  ptsize) {
	TTF_Font* tmpfont;
	tmpfont = TTF_OpenFont(file, ptsize);
	if (tmpfont == NULL) {
		cout << "|ERROR| Erro ao carregar a fonte no caminho: "<< file << "  " << TTF_GetError() << endl;
	}
	return tmpfont;
}

SDL_Surface * Text::drawtext(TTF_Font * fontTodraw, Color * fontColor, const char text[], Textquality quality)
{
	SDL_Color color = {fontColor->r,fontColor->g,fontColor->b,fontColor->alfa};

	SDL_Surface * resulting_text;

	if(quality == solid){
		resulting_text = TTF_RenderText_Solid(fontTodraw, text, color);
	}
	else if(quality == blended){
		resulting_text = TTF_RenderText_Blended(fontTodraw, text, color);
	}

	return resulting_text;
}
